﻿#region Copyright (c) 2009 S. van Deursen
/* The CuttingEdge.EntitySorting library.
 * 
 * To contact me, please visit my blog at http://www.cuttingedge.it/blogs/steven/ 
 *
 * Copyright (c) 2009 S. van Deursen
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
 * following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Linq.Expressions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CuttingEdge.ServiceLayerHelpers.Tests.Unit
{
    [TestClass]
    public class EntityFilterExtensionsTests
    {
        private readonly IEntityFilter<Person> emptyFilter = new GoodPersonEntityFilter();

        [TestMethod]
        public void Where_WithCorrectBaseFilterAndPredicate_ReturnsAValue()
        {
            // Arrange
            Expression<Func<Person, bool>> predicate = p => p.Id < 5;

            // Act
            var newFilter = EntityFilterExtensions.Where(this.emptyFilter, predicate);

            // Assert
            Assert.IsNotNull(newFilter, "EntityFilterExtensions.Where should never return null.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Where_WithNullPredicate_ThrowsException()
        {
            // Arrange
            Expression<Func<Person, bool>> predicate = null;

            // Act
            EntityFilterExtensions.Where(this.emptyFilter, predicate);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Where_WithNullBaseFilter_ThrowsException()
        {
            // Arrange
            IEntityFilter<Person> invalidFilter = null;
            Expression<Func<Person, bool>> predicate = p => p.Id < 5;

            // Act
            var newFilter = EntityFilterExtensions.Where(invalidFilter, predicate);
        }

        private sealed class GoodPersonEntityFilter : IEntityFilter<Person>
        {
            public IQueryable<Person> Filter(IQueryable<Person> collection)
            {
                return collection;
            }
        }
    }
}
